﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Bzure.Mvc.Html
{
    public static class PageExtensions
    {
        public static MvcHtmlString Pager<T>(this HtmlHelper htmlHelper, EntityPage<T> EntityPage, string ScriptFunctionName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(Script(ScriptFunctionName));
            sb.Append(FirstPage(EntityPage));
            sb.Append(PrePage(EntityPage));
            sb.Append(NextPage(EntityPage));
            sb.Append(LastPage(EntityPage));
            sb.Append(PageCountLabel(EntityPage));
            sb.Append(RecordCountLabel(EntityPage));
            return new MvcHtmlString(sb.ToString());
        }
        static string FirstPage<T>(EntityPage<T> page)
        {
            if (page.PageIndex > 0)
                return string.Format("<div onclick=\"_DoPage(1);\">{0}</div>", page.FirstButton.ToHtmlString());
            else
                return string.Format("<div>{0}</div>", page.FirstButton.ToHtmlString());
        }
        static string PrePage<T>(EntityPage<T> page)
        {
            if (page.PageIndex > 1)
                return string.Format("<div onclick=\"_DoPage({1});\">{0}</div>", page.PreButton.ToHtmlString(), page.PageIndex - 1);
            else
                return string.Format("<div>{0}</div>", page.PreButton.ToHtmlString());
        }
        static string NextPage<T>(EntityPage<T> page)
        {
            if (page.PageIndex < page.PageCount)
                return string.Format("<div onclick=\"_DoPage({1});\">{0}</div>", page.NextButton.ToHtmlString(), page.PageIndex + 1);
            else
                return string.Format("<div>{0}</div>", page.NextButton.ToHtmlString());
        }
        static string LastPage<T>(EntityPage<T> page)
        {
            if (page.PageIndex != page.PageCount)
                return string.Format("<div onclick=\"_DoPage({1});\">{0}</div>", page.LastButton.ToHtmlString(), page.PageCount);
            else
                return string.Format("<div>{0}</div>", page.LastButton.ToHtmlString());
        }
        static string Script(string ScriptFunctionName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script type=\"text/");
            sb.Append("javascript\">");
            //脚本
            sb.Append("function _DoPage(pageindex){");
            sb.Append(ScriptFunctionName + "(pageindex);");
            sb.Append("}");

            sb.Append("</script>");
            return sb.ToString();
        }
        static string PageCountLabel<T>(EntityPage<T> page)
        {
            return string.Format("<div>{0}{1}/{2}{3}</div>", page.PageIndexLabelBefore.ToHtmlString(), page.PageIndex.ToString(), page.PageCount.ToString(), page.PageIndexLabelAfter.ToHtmlString());
        }
        static string RecordCountLabel<T>(EntityPage<T> page)
        {
            return string.Format("{0}{1}{2}", page.RecordCountLabelBefor.ToHtmlString(), page.RecordCount.ToString(), page.RecordCountLabelAfter.ToHtmlString());
        }
    }
}
